package com.googlecode.thriftbenchmark.twitterprotoc;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import com.googlecode.thriftbenchmark.twitterprotoc.Twitter.TweetProtoc;
import com.googlecode.thriftbenchmark.twitterprotoc.Twitter.TweetProtoc.LocationProtoc;
import com.googlecode.thriftbenchmark.twitterprotoc.Twitter.TweetProtoc.TweetTypeProtoc;


/**
 * 
 * @author Li Weibo (weibo.leo@gmail.com) //I believe spring-brother
 */
public class TweetProtocFactory {

    private Random random = new Random();
    
    public TweetProtoc randomTweet() {
        int userId = randomUserId();
        return TweetProtoc.newBuilder().setUserId(userId)
            .setUserName("User" + userId)
            .setText(randomText())
            .setTweetType(randomTweetType())
            .setLocation(randomLocation()).build();
    }
    
    public List<TweetProtoc> randomTweets(int n) {
        List<TweetProtoc> tweets = new ArrayList<TweetProtoc>(n);
        for (int i = 0; i < n; i++) {
            tweets.add(randomTweet());
        }
        return tweets;
    }
    
    private int randomUserId() {
        return 100000 + random.nextInt(100000);
    }
    
    private LocationProtoc randomLocation() {
        return LocationProtoc.newBuilder()
            .setLatitude(180 * random.nextDouble())
            .setLongitude(360 * random.nextDouble()).build();
    }
    
    private static final String TEXT_SET = "Thrift is a software framework for scalable cross-language services development. It combines a software stack with a code generation engine to build services that work efficiently and seamlessly between C++, Java, Python, PHP, Ruby, Erlang, Perl, Haskell, C#, Cocoa, JavaScript, Node.js, Smalltalk, and OCaml.";
    private String randomText() {
        int randomLength = random.nextInt(TEXT_SET.length());
        return TEXT_SET.substring(0, randomLength);
    }

    private static TweetTypeProtoc[] ALL_TYPE = new TweetTypeProtoc[]{TweetTypeProtoc.DM, TweetTypeProtoc.REPLY, TweetTypeProtoc.RETWEET, TweetTypeProtoc.TWEET};
    private TweetTypeProtoc randomTweetType() {
        return ALL_TYPE[random.nextInt(ALL_TYPE.length)];
    }
    
}
